package sk.tuke.constraintbuilder.utils;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IParameter;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.Parameterization;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

import sk.tuke.constraintbuilder.model.ConstraintPatterns;
import sk.tuke.constraintbuilder.model.RootElement;

public class MyUtils {
	
	//Singleton object ContraintPatterns
	private static ConstraintPatterns constraintPatterns = null;
	
	public static void executeCommand(String command, IWorkbenchPartSite site){
		IHandlerService handlerService = (IHandlerService) site.getService(IHandlerService.class);
		try {
			handlerService.executeCommand(command, null);
		} catch (Exception ex) {
			throw new RuntimeException(command+" not found");
		}
	}
	
	public static void executeParametrizedCommand(String command, String parameter, String parameterValue, IWorkbenchPartSite site){
		ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
		ICommandService cmdService = (ICommandService) site.getService(ICommandService.class);
		Command cmd = cmdService.getCommand(command);
		try {
			IParameter iparam = cmd.getParameter(parameter);
			Parameterization params = new Parameterization(iparam, parameterValue);
			parameters.add(params);
			ParameterizedCommand pc = new ParameterizedCommand(cmd, parameters.toArray(new Parameterization[parameters.size()]));
			IHandlerService handlerService = (IHandlerService) site.getService(IHandlerService.class);
			handlerService.executeCommand(pc, null);
		} catch (NotDefinedException | ExecutionException | NotEnabledException | NotHandledException e) {
			throw new RuntimeException(command+" not found");
		}
	}
	
	public static String getFirstIDFromSelection(ISelection selection){
		if (selection != null && selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			// If we had a selection lets open the editor
			if (obj != null) {
				RootElement element = (RootElement) obj;
				return String.valueOf(element.getId());
			}
		}
		return null;
	}
	
	public static ConstraintPatterns getConstraintPatterns() throws JAXBException{
		if(constraintPatterns==null){
			InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(MyPreferences.PROPERTIES_FILE);
			constraintPatterns = (ConstraintPatterns) JAXBContext.newInstance(ConstraintPatterns.class).createUnmarshaller().unmarshal(input);
		}
		return constraintPatterns;
	}

}
